import ActionManagerHelperHall from "../lib/Base/ActionManagerHelperHall";
import BaseComponentByHall from "../lib/Base/BaseComponentByHall";


const { ccclass, property } = cc._decorator;

@ccclass
export default class HallUIActionTool extends BaseComponentByHall {

    @property({
        type: cc.Node,
        displayName: "上方UI组"
    })
    pn_TopList: cc.Node[] = [];
    @property({
        type: cc.Node,
        displayName: "下方UI组"
    })
    pn_DownList: cc.Node[] = [];
    @property({
        type: cc.Node,
        displayName: "左侧UI组"
    })
    pn_LeftList: cc.Node[] = [];
    @property({
        type: cc.Node,
        displayName: "右侧UI组"
    })
    pn_RightList: cc.Node[] = [];



    onLoad() {
        this.initNode(this.pn_TopList)
        this.initNode(this.pn_DownList)
        this.initNode(this.pn_LeftList)
        this.initNode(this.pn_RightList)

    }
    onEnable() {

        this.onEvent("HallUI_Hide")
        this.onEvent("HallUI_Show")
    }
    onDisable() {
        this.offEvent("HallUI_Hide")
        this.offEvent("HallUI_Show")
    }
    onDestroy() {

    }
    HallUI_Show() {
        let dt = 0.5
        this.MoveNode(dt, this.pn_TopList, 0, 0)
        this.MoveNode(dt, this.pn_DownList, 0, 0)
        this.MoveNode(dt, this.pn_LeftList, 0, 0)
        this.MoveNode(dt, this.pn_RightList, 0, 0)
    }
    HallUI_Hide() {
        let dt = 0.5
        let offset = 200
        this.MoveNode(dt, this.pn_TopList, 0, offset)
        this.MoveNode(dt, this.pn_DownList, 0, -offset)
        this.MoveNode(dt, this.pn_LeftList, -offset, 0)
        this.MoveNode(dt, this.pn_RightList, offset, 0)
    }


    /** 初始化节点  保存初始位置
     * 
     * @param list 
     */
    initNode(list) {
        list || (list = [])
        for (let i = 0, len = list.length; i < len; i++) {
            let node = list[i];
            node.OldX = node.x;
            node.OldY = node.y;
        }
    }
    /** 移动节点
     * 
     * @param dt 持续时间
     * @param list 节点组
     * @param ox 偏移量
     * @param oy 偏移量
     */
    MoveNode(dt, list, ox, oy) {
        for (let i = 0, len = list.length; i < len; i++) {
            let node = list[i];
            if (!cc.isValid(node)) {
                continue;
            }
            let act = cc.moveTo(dt, cc.v2(node.OldX + ox, node.OldY + oy)).easing(cc.easeCircleActionInOut());
            node.stopAllActions();
            //维护此act  防止切后台发生异常
            ActionManagerHelperHall.addAction(act, node)
        }
    }


}
